package hedron.view;

import hedron.Activator;
import hedron.editor.ContactsEditor;
import hedron.editor.__DemoEditor3;
import hedron.editor.__DemoEditorPaged;
import hedron.editorinput.DummyEditorInput;
import java.util.ArrayList;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.part.ViewPart;

public class DemoView extends ViewPart implements ITreeContentProvider, ILabelProvider, IDoubleClickListener {
	public static final String ID = "HEDRON.DemoView";

	TreeViewer treeMenu;
	ArrayList<TreeItem> treeItems = new ArrayList<TreeItem>();

	static int democounter = 1;

	@Override
	public void createPartControl(Composite parent) {
		TreeItem node;
		treeItems.add(node = new TreeItem("", null));
		treeItems.add(node = new TreeItem("HEDRON", node));
		treeItems.add(new TreeItemAction("Customers", node) {
			void action() throws Exception {
				IWorkbenchPage page = getSite().getPage();
				page.openEditor(new DummyEditorInput("-1", "-1"), ContactsEditor.ID);
			}
		});
		treeItems.add(new TreeItemAction("Items", node) {
			void action() throws Exception {
				IWorkbenchPage page = getSite().getPage();
				page.openEditor(new DummyEditorInput("0", "0"), __DemoEditorPaged.ID);
			}
		});
		treeItems.add(new TreeItemAction("Invoices", node) {
			void action() throws Exception {
				IWorkbenchPage page = getSite().getPage();
				page.openEditor(new DummyEditorInput("" + democounter++, ""), __DemoEditor3.ID);
			}
		});

		treeItems.add(node = new TreeItem("REPORTS", treeItems.get(0)));
		treeItems.add(new TreeItem("Incomes", node));
		treeItems.add(new TreeItem("Outcomes", node));

		treeMenu = new TreeViewer(parent);
		treeMenu.setContentProvider(this);
		treeMenu.setLabelProvider(this);
		treeMenu.setInput(treeItems.get(0));
		treeMenu.expandToLevel(treeItems.get(1), 2);
		treeMenu.addDoubleClickListener(this);
	}

	@Override
	public void doubleClick(DoubleClickEvent event) {
		// page =
		// PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		// else if in ViewPart // page = getSite ().getPage ();
		try {
			TreeSelection selection = (TreeSelection) treeMenu.getSelection();
			if (!selection.isEmpty()) {
				Object element = selection.getFirstElement();
				if (element instanceof TreeItemAction) {
					((TreeItemAction) element).action();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static abstract class TreeItemAction extends TreeItem {
		public TreeItemAction(String name, TreeItem parent) {
			super(name, parent);
		}

		abstract void action() throws Exception;
	}

	public static class TreeItem {
		public String name;
		public TreeItem parent;
		ArrayList<TreeItem> children = new ArrayList<TreeItem>();

		public TreeItem(String name, TreeItem parent) {
			this.name = name;
			this.parent = parent;
			if (parent != null) {
				parent.children.add(this);
			}
		}

		@Override
		public int hashCode() {
			return name.hashCode();
		}
	}

	@Override
	public void setFocus() {}

	@Override
	public Object[] getChildren(Object parentElement) {
		return ((TreeItem) parentElement).children.toArray();
	}

	@Override
	public Object getParent(Object element) {
		return ((TreeItem) element).parent;
	}

	@Override
	public boolean hasChildren(Object element) {
		return ((TreeItem) element).children.size() > 0;
	}

	@Override
	public Object[] getElements(Object inputElement) {
		return ((TreeItem) inputElement).children.toArray();
	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}

	@Override
	public void addListener(ILabelProviderListener listener) {}

	@Override
	public boolean isLabelProperty(Object element, String property) {
		return false;
	}

	@Override
	public void removeListener(ILabelProviderListener listener) {}

	@Override
	public String getText(Object element) {
		return ((TreeItem) element).name;
	}

	@Override
	public Image getImage(Object element) {
		String imageName = null;
		if (((TreeItem) element).parent.parent == null) {
			imageName = "icons/dir.png";
		} else {
			imageName = "icons/file.png";
		}
		return Activator.getImage(imageName);
	}

	public void dispose() {}
}
